<template>
  <div>
    <el-card class="box-card">
      <el-form ref="form" :model="form" label-width="120px">
        <el-divider content-position="left">基础设置</el-divider>
        <el-row :gutter="10">
          <el-col :span="8">
            <el-form-item label="是否可设置" prop="isSet">
              <el-radio-group v-model="form.isSet">
                <el-radio
                  v-for="item in this.dict.whether_type"
                  :key="item.id"
                  :label="item.value"
                >{{ item.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否显示图标" prop="sfxstb">
              <el-radio-group v-model="form.sfxstb">
                <el-radio
                  v-for="item in this.dict.whether_type"
                  :key="item.id"
                  :label="item.value"
                >{{ item.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="tab最大个数" prop="xsts">
              <el-input-number v-model="form.xsts" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="custom-button">
        <el-button type="primary" size="small" :loading="loading" icon="el-icon-circle-check" @click="saveEntity">保存
        </el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import { adminQueryTemplate, adminSaveOrUpdate } from '@/api/template/tabs';
export default {
  name: 'Tabs',
  dicts: ['whether_type'],
  props: {
    template: {
      type: Number,
      require: true
    }
  },
  data() {
    return {
      loading: false,
      form: {
        templateId: this.template,
        isSet: 'false', // 是否可设置
        sfxstb: 'false', // 是否显示更多
        xsts: 1
      },
      defaultForm: {
        templateId: this.template,
        isSet: 'false', // 是否可设置
        sfxstb: 'false', // 是否显示更多
        xsts: 1
      }
    };
  },
  watch: {
    template: {
      handler(newName, oldName) {
        if (newName != null && undefined !== newName) {
          this.initEntity();
        }
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  methods: {
    initEntity() {
      adminQueryTemplate({ 'tid': this.template }).then(res => {
        if (res != null && res !== '') {
          this.form = res;
          this.form.isSet = this.form.isSet.toString();
          this.form.sfxstb = this.form.sfxstb.toString();
        } else {
          this.form = this.defaultForm;
        }
      }).catch(error => {
        console.log(error);
      });
    },
    saveEntity() {
      this.loading = true;
      adminSaveOrUpdate(this.form).then(res => {
        this.$message({
          message: '配置项保存成功！',
          type: 'success'
        });
        this.loading = false;
      }).catch(() => {
        this.$message.error('保存失败！请联系系统管理员！');
        this.loading = false;
      });
    }
  }
};
</script>

<style scoped>
.custom-button {
  width: 100%;
  height: 40px;
  text-align: center;
}
</style>
